package com.manager.plat.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.manager.plat.dao.IPartnerUpperDao;
import com.manager.plat.util.BaseUtil;
import com.manager.plat.vo.PartnerUpperVo;

public class PartnerUpperDaoImpl extends JdbcDaoSupport implements IPartnerUpperDao {
	private static Logger logger = Logger.getLogger(PartnerUpperDaoImpl.class);
	/**查询所有有效上游信息列表*/
	@SuppressWarnings("unchecked")
	@Override
	public List<PartnerUpperVo> getPartnerUpperList() {
		String sql = "select id, name, detail, bindip from tb_ivr_partner_upper where status = 1";
		
		logger.debug(String.format("查询所有有效上游信息列表[%s]", sql));
		List<PartnerUpperVo> partnerUpperVoList = this.getJdbcTemplate().query(sql, rowMapper);
		if(partnerUpperVoList != null && partnerUpperVoList.size() > 0){
			return partnerUpperVoList;
		}
		return null;
	}
	/**根据Id获得上游信息*/
	@SuppressWarnings("unchecked")
	@Override
	public PartnerUpperVo getPartnerUpperById(int id) {
		String sql = "select id, name, detail, bindip from tb_ivr_partner_upper where id = ? and status = 1";
		Object[] param = new Object[]{id};
		logger.debug(String.format("查询所有有效上游信息列表[%s]", BaseUtil.logSQL(sql, param)));
		List<PartnerUpperVo> partnerUpperVoList = this.getJdbcTemplate().query(sql, param, rowMapper);
		if(partnerUpperVoList != null && partnerUpperVoList.size() > 0){
			return partnerUpperVoList.get(0);
		}
		return null;
	}
	
	private ParameterizedRowMapper<PartnerUpperVo> rowMapper = new ParameterizedRowMapper<PartnerUpperVo>() {

		@Override
		public PartnerUpperVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			PartnerUpperVo partnerUpperVo = new PartnerUpperVo();
			partnerUpperVo.setId(rs.getInt("id"));
			partnerUpperVo.setName(rs.getString("name"));
			partnerUpperVo.setDetail(rs.getString("detail"));
			partnerUpperVo.setBindip(rs.getString("bindip"));
			return partnerUpperVo;
		}
	};
	
	/**保存新的上游信息*/
	@Override
	public int insertPartnerUpper(PartnerUpperVo partnerUpperVo) {
		String sql = "insert into tb_ivr_partner_upper(name,detail,bindip,status,add_user) values(?,?,?,1,?)";
		Object[] param = new Object[]{partnerUpperVo.getName(), partnerUpperVo.getDetail(),
									partnerUpperVo.getBindip(), partnerUpperVo.getAdd_user()};
		
		logger.debug(String.format("保存新的上游信息[%s]", BaseUtil.logSQL(sql, param)));
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**更新上游信息*/
	@Override
	public int updatePartnerUpper(PartnerUpperVo partnerUpperVo) {
		String sql = "update tb_ivr_partner_upper set name = ?, detail = ?, bindip = ?, update_date = now(), update_user = ? where id = ?";
		Object[] param = new Object[]{partnerUpperVo.getName(), partnerUpperVo.getDetail(),
									partnerUpperVo.getBindip(), partnerUpperVo.getUpdate_user(),
									partnerUpperVo.getId()};
		
		logger.debug(String.format("更新上游信息[%s]", BaseUtil.logSQL(sql, param)));
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**删除上游信息*/
	@Override
	public int deletePartnerUpper(int id, int update_user) {
		String sql = "update tb_ivr_partner_upper set status = 0, update_date = now(), update_user = ? where id = ?";
		Object[] param = new Object[]{update_user, id};
		logger.info(String.format("删除上游信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
}
